package com.lik.javadeep.basic.sort;

public class BubbleSort2 {
    public static void main(String[] args) {
        int[] ints = {9, 3, 5, 2, 9, 6, 18};
        int lo = 0;
        int end = ints.length - 1;

        while (lo < (end = bubble(ints, lo, end))) {

        }

        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    /**
     * 遍历一次，返回已经有序的（last,end）
     *
     * @param array
     * @param end
     * @return
     */
    private static int bubble(int[] array, int lo, int end) {
        int last = lo;
        for (int j = lo; j < end; j++) {
            if (array[j + 1] < array[j]) {
                swap(array, j, j + 1);
                last = j;
            }
        }

        return last;
    }

    private static void swap(int[] array, int aIndex, int bIndex) {
        int temp = array[aIndex];
        array[aIndex] = array[bIndex];
        array[bIndex] = temp;
    }
}
